﻿@using fqlmoney.Model
@{
    ViewBag.Title = "账单清单";
    ViewBag.Menu = "order";
    List<user_order> list = ViewBag.list;
    List<user_moneytype> typelist = ViewBag.typelist;
}
@section fqlmoneyCss{
<link href="/Content/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
    <style>
       .m-b {
            margin-bottom: 15px;
        }
       .b-p{padding:0px 5px;}
        .pull-in {
            margin-left: -15px;
            margin-right: -15px;
        }
        .bs-example {
            position: relative;
            padding: 20px 35px 15px;
            margin: 0 -15px 15px;
            border-style: solid;
            margin-right: 0;
            margin-left: 0;
            background-color: #fff;
            border-color: #ddd;
            border-width: 1px;
            border-radius: 4px 4px 0 0;
            -webkit-box-shadow: none;
            box-shadow: none;
        }
    </style>
}
<div class="row">
    <div class="col-sm-12">
        <section class="panel panel-default">
            <header class="panel-heading">
                记一笔
            </header>
            <div class="panel-body">
                <div class="form-group pull-in clearfix">
                    <div class="col-sm-3">
                        <div class="input-group m-b">
                            <span class="input-group-addon">收支</span>
                            <select class="form-control input-sm" id="orderstate" name="orderstate">
                                <option value="2">支出</option>
                                <option value="1">收入</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-sm-3">
                        <div class="input-group m-b">
                            <span class="input-group-addon">分类</span>
                            <select class="form-control input-sm" id="type" name="type">
                                @foreach (var item in typelist)
                                {
                                    if (item.t_state == 1)
                                    {
                                        <option value="@item.t_id" data-state="@item.t_state" style="display:none;">@item.t_name</option>
                                    }
                                    else
                                    {
                                        <option value="@item.t_id" data-state="@item.t_state">@item.t_name</option>
                                    }

                                }
                            </select>
                        </div>
                    </div>
                    <div class="col-sm-3">
                        <div class="input-group m-b">
                            <span class="input-group-addon">日期：</span>
                            <input class="form-control input-sm valid" id="dates" name="dates" placeholder="时间不能为空" type="text" value="@DateTime.Now.ToString("yyyy-MM-dd")" >
                            <span class="input-group-addon" title="点前一天,时间将会自动变成前一天" style="cursor: pointer;" onclick="last()">前一天</span>
                        </div>
                    </div>
                   
                    <div class="col-sm-3">
                        <div class="input-group m-b">
                            <span class="input-group-addon text-danger">金额</span>
                            <input class="form-control input-sm" id="amount" name="amount" placeholder="0" type="text" value="">
                            <span class="input-group-addon"><i class="fa fa-jpy text-danger"></i></span>
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="input-group m-b">
                            <span class="input-group-addon">备注</span>
                            <input class="form-control" id="ordernote" maxlength="50" name="ordernote" type="text" value="">
                        </div>
                    </div>
                </div>
            </div>
            <footer class="panel-footer text-right bg-light lter">
                <div class="row">
                    <div class="col-sm-offset-9 col-sm-3 text-right">
                        <input type="hidden" id="orderid" value="0" />
                        <button class="btn btn-default canclesave">放弃保存</button>
                        <button class="btn btn-success ordersave">新增一笔</button>
                    </div>
                </div>
            </footer>
        </section>
     </div>
    <div class="col-sm-12">
                <div class="bs-example" data-example-id="inline-code">


                    <div class="table-responsive">
                        <table class="table table-condensed">
                            <thead>
                                <tr>
                                    <td>日期</td>
                                    <td>类别</td>
                                    <td>收支</td>
                                    <td>金额</td>
                                    <td>备注</td>
                                    <td>删除</td>
                                    <td>修改</td>
                                </tr>
                            </thead>
                            <tbody>

                                @foreach (var item in list)
                                {
                                    <tr>
                                        <td>@item.o_time.ToString("yyyy-MM-dd")</td>
                                        <td>@item.t_name</td>
                                        <td>@(item.o_state==1?"收入":"支出")</td>
                                        <td>@item.o_amount</td>
                                        <td>@item.o_note</td>
                                        <td><button type="button" data-oid="@item.o_id" class="btn btn-danger b-p orderdel"><i class="fa fa-times"></i></button></td>
                                        <td><button type="button" data-oid="@item.o_id" data-tid="@item.t_id" data-state="@item.o_state" data-amount="@item.o_amount" data-note="@item.o_note" data-time="@item.o_time.ToString("yyyy-MM-dd")" class="btn btn-info b-p orderedit"><i class="fa fa-edit"></i></button></td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                        @Html.Raw(fqlmoney.Common.MyHtmlHelper.ShowPageNavigate(ViewBag.request_url,ViewBag.page, (int)ViewBag.pagesize, (long)ViewBag.totalcount, (string)ViewBag.search_url))
                    </div>
                </div>
            </div>

    </div>
@section fqlmoneyScript{

<script src="/Content/script/bootstrap-datetimepicker.min.js"></script>

   <script>
       function last() {
           var obj = $("#dates").val();
           if (obj == "") {
               return;
           }
           obj = obj.replace(/-/g, '/'); // 将-替换成/，因为下面这个构造函数只支持/分隔的日期字符串
           var currDate = new Date(obj);
           currHours = currDate.getHours();
           nextHours = currDate.setHours(currHours - 24);//减1天
           d = new Date(nextHours);
           var m = (d.getMonth() + 1);
           var day=d.getDate();
           if (m<10)
           {
               m = "0" + m;
           }
           if (day<10)
           {
               day = "0" + day;
           }
           var s = '';
           s += d.getFullYear();    // 获取年份。
           s += "-" + m;            // 获取月份。
           s += "-" + day;          // 获取日。
           $("#dates").val(s);
       }
       $(function () {
           $("#orderstate").change(function () {
               var state = $(this).val();
               var showlist = $("#type option[data-state=" + state + "]");
               if (showlist.length == 0)
               {
                   alert("分类为空！请添加分类");
                   $("#type").val("");
                   $("#type option").hide();
               }
               else {
                   $("#type").val(showlist[0].value);
                   $("#type option").hide();
                   showlist.show();
               }
           });

           $("#dates").datetimepicker({
               format: 'yyyy-mm-dd',
               weekStart: 1,
               todayBtn: 1,
               autoclose: 1,
               todayHighlight: 1,
               startView: 2,
               minView: 2,
               forceParse: 0
           });
            $("button.canclesave").click(function () {
                $("#orderid").val(0);
                $("#amount").val();
                $("#ordernote").val("");
                $(".typesave").text("新增一笔");
            });
            $("button.orderedit").click(function () {
                var obj = $(this);
                $("#orderid").val(obj.attr("data-oid"));
                $("#orderstate").val(obj.attr("data-state"));
                $("#type").val(obj.attr("data-tid"));
                $("#amount").val(obj.attr("data-amount"));
                $("#ordernote").val(obj.attr("data-note"));
                $("#dates").val(obj.attr("data-time"));
                $(".ordersave").text("编辑保存");
            });

            $("button.ordersave").click(function(){
                var oid = $("#orderid").val();
                var tid = $("#type").val();
                var time = $("#dates").val();
                var state = $("#orderstate").val();
                var note = $("#ordernote").val();
                var amount = $("#amount").val();
                var reg = new RegExp("^[0-9]+\.?[0-9]*$");
                if (tid == "")
                {
                    $("#type").focus();
                    return;
                }
                if (time == "") {
                    $("#dates").focus();
                    return;
                }
                if (state == "") {
                    $("#orderstate").focus();
                    return;
                }
                if (amount == "" || amount==0) {
                    $("#amount").focus();
                    return;
                }
                if (!reg.test(amount)) {
                    $("#amount").focus();
                    return;
                }
                var list = { "o_id": oid, "t_id": tid, "o_time": time, "o_state": state, "o_note": note, "o_amount": amount };
                var url="";
                if (oid == 0)
                {
                    url="/Order/Add";
                }
                else{
                    url="/Order/Update";
                }
                $.post(url,list,function(data){
                    alert(data.msg);
                    location.reload();
                });
            });

            $("button.orderdel").click(function(){
                if(confirm('确定删除该条数据'))
                {
                    $.post("/Order/Delete",{"id":$(this).attr("data-oid")},function(data){
                        alert(data.msg);
                        location.reload();
                    });
                }
            });
        });

</script>
}
